package elasticSearch;

import com.alibaba.fastjson.JSON;
import entity.Medicine;
import org.apache.lucene.queryparser.flexible.core.builders.QueryBuilder;
import org.apache.lucene.queryparser.xml.FilterBuilder;
import org.apache.lucene.queryparser.xml.builders.BooleanQueryBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;

import javax.naming.directory.SearchResult;
import java.util.Map;

/**
 * Created by cdxuhongya on 2016/9/22.
 */
public class ElasticSearchBasic {

    /**
     * es服务器的host
     */
    private static final String host = "127.0.0.1";

    /**
     * es服务器暴露给client的port
     */
    private static final int port = 9300;

    /**
     * java客户端
     */
    private static Client client = getClient();

    /**
     *
     * 索引，默认索引，类似于数据库中的表的概念
     *
    * */
    private final static String DEFAULT_INDEX = "default_index";

    /**
     *
     * 类型，默认类型，类似于数据库中的表中的一行数据的概念
     *
     * */
    private final static String DEFAULT_TYPE = "default_type";


    /**
     * 索引一条数据，类似于数据库中插入一条数据内容
     *
     * */
    public void indexDataById(Integer id,String jsonData){
        IndexResponse response = client.prepareIndex(DEFAULT_INDEX, DEFAULT_TYPE,id.toString())
                .setSource(jsonData)
                .execute()
                .actionGet();
    }

    /**
     * 索引一条数据，类似于数据库中插入一条数据内容
     *
     * */
    public void indexDataById(Integer id,Map<String,String> map){
        IndexResponse response = client.prepareIndex(DEFAULT_INDEX, DEFAULT_TYPE,id.toString())
                .setSource("id",map.get("id"),"key",map.get("key"),"field",map.get("field"))
                .execute()
                .actionGet();
    }



    /**
     *
     * 根据ID获取一条数据,类似于从数据库中查询一条数据
     * */
    public GetResponse getResponseById(Integer id){
        GetResponse response = client.prepareGet(DEFAULT_INDEX, DEFAULT_TYPE,id.toString())
                .setOperationThreaded(false)
                .execute()
                .actionGet();
        return response;
    }


    /**
     *
     * 根据ID获取一条数据,类似于从数据库中查询一条数据
     * */
    public DeleteResponse deleteIndexById(Integer id){
        DeleteResponse response = client.prepareDelete(DEFAULT_INDEX, DEFAULT_TYPE, id.toString())
                .setOperationThreaded(false)
                .execute()
                .actionGet();
        return response;
    }

    /**
     *
     * 搜索数据内容
     *
     * */
    public SearchResponse searchByKey(String key){
        org.elasticsearch.index.query.QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "中药");
        SearchResponse searchResponse = client.prepareSearch(DEFAULT_INDEX)
                .setTypes(DEFAULT_TYPE)
                .setSearchType(SearchType.QUERY_AND_FETCH)
                .setQuery(QueryBuilders.termQuery("id","1800"))
                //.setPostFilter(FilterBuilders.rangeFilter("id").from(18000).to(2000))
                //.setQuery(queryBuilder)
                .execute()
                .actionGet();
        return searchResponse;

    }

    /**
     *
     * 搜索数据内容
     *
     * */
    public SearchResponse searchByKeyMust(String key){

        SearchRequestBuilder builder = client.prepareSearch(DEFAULT_TYPE).setTypes(DEFAULT_INDEX).setSearchType(SearchType.DEFAULT)
                .setFrom(0).setSize(100);

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder("12000").field("id"));
        builder.setQuery(queryBuilder);

        SearchResponse searchResponse = builder.execute().actionGet();
        return searchResponse;

    }


    /**
     *
     * 获取elasticsearch的java的连接客户端
     *
     * */
    public static Client getClient(){
        Settings settings = ImmutableSettings.settingsBuilder()
                .put("cluster.name", "testes")
                .put("node.name", "test").build();
        Client client = new TransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(host, port));

        return client;
    }
}
